/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: exec_error_domain.h
 *
 * Purpose: The Execution Management cluster implements an error handling based on
 *          ara::core::Result. The errors supported by the Execution Management 
 *          cluster are listed in section 8.3.1
 *
 * Developer:
 *   wen.gu , 2020-04-20
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __ARA_EXEC_EXEC_ERROR_DOMAIN_H__
#define __ARA_EXEC_EXEC_ERROR_DOMAIN_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include <cstdint>
#include <memory>

#include "ara/core/result.h"
#include "ara/core/core_error_domain.h"
#include "ara/core/exception.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/

#ifndef EXEC_ERR_CASE
#define EXEC_ERR_CASE(err) case err: ret = #err; break
#endif
/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace ara
{
namespace exec
{
enum class ExecErrc : ara::core::ErrorDomain::CodeType
{
    kGeneralError = 1, /** Some unspecified error occurred */
    kInvalidArguments = 2, /** Invalid argument was passed */
    kCommunicationError = 3, /** Communication error occurred */
    kMetaModelError = 4, /** Wrong meta model identifier passed to a function */
    kCancelled = 5, /** Transition to the requested Function Group state
                      * was cancelled by a newer request */
    kFailed = 6, /** Transition to the requested Function Group state failed */
};
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

class ExecException : public ara::core::Exception
{
public:
    explicit ExecException(ara::core::ErrorCode errorCode) noexcept
        :Exception(errorCode)
    {
        /** todo something */
    }
};

class ExecErrorDomain final : public ara::core::ErrorDomain
{
public:
    using Errc = ExecErrc;
    using Exception = ExecException;

public:
    constexpr ExecErrorDomain() noexcept
        :ara::core::ErrorDomain(0x8000000000000300ULL)
    {
        /** todo something */
    }

public:
    char const* Name() const noexcept override { return "EXEC"; } /**todo refineme?? */
    char const* Message(CodeType errorCode) const noexcept override
    {
        char const* ret = "";
        Errc err = static_cast<Errc>(errorCode);
        switch (err)
        {
        CORE_ERR_CASE(Errc::kGeneralError);
        CORE_ERR_CASE(Errc::kInvalidArguments);
        CORE_ERR_CASE(Errc::kCommunicationError);
        CORE_ERR_CASE(Errc::kMetaModelError);
        CORE_ERR_CASE(Errc::kCancelled);
        CORE_ERR_CASE(Errc::kFailed);
        default: break;
        }

        return ret;
    }

    void ThrowAsException(ara::core::ErrorCode const& errorCode) const noexcept(false) override
    {
        throw Exception(errorCode);
    }
public:
    //static constexpr FutureErrorDomain mErrDomain;

    static const ExecErrorDomain& GetInstance()
    {
        constexpr static const ExecErrorDomain fem;
        return fem;
    }

    constexpr ExecErrorDomain const& GetRef() const { return *this; }
};



constexpr inline ara::core::ErrorDomain const& GetExecErrorDomain() noexcept
{
    return ExecErrorDomain::GetInstance().GetRef();
}


constexpr inline ara::core::ErrorCode MakeErrorCode(ara::exec::ExecErrc code, ara::core::ErrorDomain::SupportDataType data) noexcept
{
    return std::move(ara::core::ErrorCode(static_cast<ara::core::ErrorDomain::CodeType>(code), GetExecErrorDomain(), data));
}


} /** namespace exec */
} /** namespace ara */

#endif /** !__ARA_EXEC_EXEC_ERROR_DOMAIN_H__ */

